var fs=require('fs');
var gd=require('gd');
/*
	About: Description
	This file contains controllers concerned with working with Tfiles and Tfile_groups tables
	and rendering galleries, file downloaders and suchs.
*/

/*****************************************************************************/
exports.controllers={
	/*
		Function: controllers.gallery
		
		Generic gallery.

		Parameters:
			options - an object of:
			options.template - optional, html template to render navigation, located in "widgets/gallery/" folder, default to "view"
			options.root_name - optional, a name in Tfile_groups to presume as root
			options.root_code - optional, a code in Tfile_groups to presume as root
			options.root_id - optional, an id of file_group to presume as root
			options.no_names - optional, if template checks, hides image names
		
		Returns:
			both a space.output and an valid html return
	 */
	gallery: function(space,options,root_name) {
		if (!options) options={template:"view"};
		if (!options.template) options.template="view";
		if (typeof(options)=="string") options={template:options};
		if (root_name) options.root_name=root_name;
		options.root_id=space.functions.get_file_group_id(space,options,"root");
		
		var fg_id=space.fields.file_group_id;
		if (!fg_id) fg_id=options.root_id;
		space.gallery_fg_id=fg_id;
		space.gallery_options=options;
		
		space.gallery_nav_full=space.site.sql.execute_and_fetch_h("file_groups/list_h",{parent_id:options.root_id});
		space.gallery_nav_full.forEach(function(x) {
			x.active=(x.id==fg_id)?1:0;
		});
		space.gallery_nav=space.site.sql.execute_and_fetch("file_groups/list_h",{parent_id:fg_id});
		space.gallery_nav.forEach(function(x) {
			var gf=space.site.sql.execute_and_fetch_one("files/get_random",{file_group_id: x.id, file_type_id: 1});
			if (!gf) return;
			var fgpath=space.functions.file_group_path(space,x.id);
			x.image_url=fgpath+gf.id+"."+gf.ext;
		});
		
		var fgpath=space.functions.file_group_path(space,fg_id);
		
		space.gallery_folders=space.site.sql.execute_and_fetch("file_groups/list_h",{parent_id:fg_id});
		space.gallery_images=space.site.sql.execute_and_fetch("files/list",{file_group_id:fg_id,file_type_id:1});
		var first;
		space.gallery_images.forEach(function(a) {
			if (!first) first=a;
			a.filename_only=fgpath+a.id;
			a.image_url=fgpath+a.id+"."+a.ext;
			if (a.id==space.fields.file_id) space.gallery_image=a;
		});
		if (!space.gallery_image) space.gallery_image=first;
		return space.output=space.views.process("widgets/gallery/"+options.template,space);
	},

	/*
		Function: controllers.gallery_photo
		
		Dont know??? TODO

		Parameters:

	*/
	gallery_photo: function(space,vars) {
		space.gallery_image=space.site.sql.execute_and_fetch_one("files/get");
		var fg_id=space.gallery_image.file_group_id;
		space.gallery_image_prev=space.site.sql.execute_and_fetch_one("files/get_prev",{file_group_id:fg_id,file_type_id:1});
		space.gallery_image_next=space.site.sql.execute_and_fetch_one("files/get_next",{file_group_id:fg_id,file_type_id:1});
		var fgpath=space.functions.file_group_path(space,fg_id);
		space.gallery_images=space.site.sql.execute_and_fetch("files/list",{file_group_id:fg_id,file_type_id:1});
		space.gallery_images.forEach(function(a) {
			a.filename_only=fgpath+a.id;
		});
		space.gallery_image.filename_only=fgpath+space.gallery_image.id;
		space.gallery_image.path=fgpath;
		return "";
	},

	/*
		Function: controllers.gallery_nav

		Parameters:
			options - an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, defaults to "gallery/nav"
			options.folder_name - optional, a name in Tfile_groups to user as folder
			options.folder_code - optional, a code in Tfile_groups to use as folder
			options.folder_id - optional, an id of file_group to use as folder
	*/
	gallery_nav: function(space,options) {
		if (!options) options={};
		if (!options.folder_code) options.folder_code="galleries";
		if (!options.template) options.template="gallery/nav";
		var fg_id=space.functions.get_file_group_id(space,options);
		space.gallery_nav=space.site.sql.execute_and_fetch_h("file_groups/list_h",{parent_id:fg_id});
		space.gallery_nav.forEach(function(x) {
			x.active=(x.id==space.fields.file_group_id)?1:0;
			var img=space.site.sql.execute_and_fetch_one("files/get_random",{file_group_id:x.id});
			if (img) x.image_url=space.functions.file_group_path(space,x.id)+img.id+"."+img.ext;
		});
		return space.views.process("widgets/"+options.template,space);
	},
	
	gallery_groups: function(space,options) {
	},
	/*
		Function: controllers.last_file

		Searches for last file in file_group and displays it.
		One of folder_id, folder_code or folder_name must be set

		Parameters:
			options - the hash of:
			options.folder_id - (optional) 
			options.folder_code - (optional) 
			options.folder_name - (optional) 
			options.template - template name in "widgets/" folder

		Returns:
			Valid HTML
	 */
	last_file: function(space,options) {
		var fg_id=space.functions.get_file_group_id(space,options);
		if (!fg_id) return "";
		var file=space.site.sql.execute_and_fetch_one("files/get_last",{file_type_id:null,file_group_id:fg_id});
		var basepath=space.site.paths.html;
		var fgpath=space.functions.file_group_path(space,fg_id);
		file.filename=fgpath+file.id+"."+file.ext;
		if (!options.template) {
			return file.filename;
		}
		space.last_file=file;
		return space.views.process("widgets/"+options.template,space);
	},
	/*
		Function: controllers.gallery_images

		Shows images in some file_group.
		One of folder_id, folder_code or folder_name must be set.

		Parameters:
			options - an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, default to "gallery/images"
			options.folder_name - optional, a name in Tfile_groups to user as folder
			options.folder_code - optional, a code in Tfile_groups to use as folder
			options.folder_id - optional, an id of file_group to use as folder
	 */
	gallery_images: function(space,options) {
		space.download_files=[];
		var fg_id=space.functions.get_file_group_id(space,options);
//		return space.Dumper(options);
		var basepath=space.site.paths.html;
		if (fg_id) {
			space.file_group=space.mfact.get("file_group",fg_id);
			if (!space.file_group) return "";
			space.gallery_images=space.site.sql.execute_and_fetch("files/list",{file_type_id:1,file_group_id:fg_id});
			var fgpath=space.functions.file_group_path(space,fg_id);
			var first;
			space.gallery_images.forEach(function(a) {
				if (!first) first=a;
				a.filename=fgpath+a.id+"."+a.ext;
			});
			space.gallery_image_first=first;
		}
		return space.views.process("widgets/"+(options.template||"gallery/images"),space);
	},

	/*
		Function: controllers.gallery_images_nojs

		Shows images in some file_group.
		One of folder_id, folder_code or folder_name must be set.

		Parameters:
			options - an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, default to "gallery/images_nojs"
			options.folder_name - optional, a name in Tfile_groups to user as folder
			options.folder_code - optional, a code in Tfile_groups to use as folder
			options.folder_id - optional, an id of file_group to use as folder
			options.limit - optional, count of preview images, defaults to 5
			options.gaction - optional,
	 */
	gallery_images_nojs: function(space,options) {
		if (!options) options={};
		if (!options.limit) options.limit=5;
		if (!("big" in options)) options.big="5";
		if (!("small" in options)) options.small="8C";
		space.download_files=[];
		var fg_id=space.functions.get_file_group_id(space,options) || space.fields.folder_id;
		var basepath=space.site.paths.html;
		var n=(space.fields.n||0)*1;
		if (fg_id) {
			space.file_group=space.mfact.get("file_group",fg_id);
			if (!space.file_group) return "";
			var gallery_images=space.site.sql.execute_and_fetch("files/list",{file_type_id:1,file_group_id:fg_id});

			var halfsize=Math.round((options.limit-1)/2);
			var pos=n-halfsize;
			if (pos<0) pos=0;
			if (pos+options.limit>gallery_images.length) pos=gallery_images.length-options.limit;
			if (pos<0) pos=0;
			space.gallery_images=gallery_images.slice(pos,pos+options.limit);
			var fgpath=space.functions.file_group_path(space,fg_id);
			space.gallery_images.forEach(function(a) {
				a.filename=fgpath+a.id+"."+a.ext;
				a.filename_big=fgpath+a.id+(options.big?"-preview"+options.big:"")+"."+a.ext;
				a.filename_small=fgpath+a.id+(options.small?"-preview"+options.small:"")+"."+a.ext;
				space.n=pos;pos++;
				a.href=space.views.apply(options.gaction,space);
			});
			if (n<0) n=0;
			if (n>=gallery_images.length) n=space.gallery_images.length-1;
			space.gallery_image_main=gallery_images[n];
			space.gallery_image_main.active=1;
			if (n>0) {space.n=n-1;space.prev=space.views.apply(options.gaction,space);}
			if (n<gallery_images.length-1) {space.n=n+1;space.next=space.views.apply(options.gaction,space);}
			space.gallery_image_main.filename=fgpath+space.gallery_image_main.id+"."+space.gallery_image_main.ext;
//			throw space.gallery_image_main;
		}
		return space.views.process("widgets/"+(options.template||"gallery/images_nojs"),space);
	},

	/*
		Function: controllers.file_downloader

		Shows files (images and not) in some file_group.
		One of folder_id, folder_code or folder_name must be set.

		Parameters:
			options - an object of:
			options.template - optional, html template to render navigation, located in "widgets/" folder, default to "gallery/file_downloader"
			options.folder_name - optional, a name in Tfile_groups to user as folder
			options.folder_code - optional, a code in Tfile_groups to use as folder
			options.folder_id - optional, an id of file_group to use as folder
	 */
	file_downloader: function(space,options) {
		space.download_files=[];
		var fg_id=space.functions.get_file_group_id(space,options);
		if (fg_id) {
			space.download_files=space.site.sql.execute_and_fetch("files/list",{file_type_id:null,file_group_id:fg_id});
			var fgpath=space.functions.file_group_path(space,fg_id);
			space.download_files.forEach(function(a) {
				a.filename=fgpath+a.id+"."+a.ext;
				a.icon=(a.type_id==1)?fgpath+a.id+"-preview2."+a.ext:space.functions.guess_icon(space,a.ext);
			});
		}
		return space.views.process("widgets/"+(options.template||"gallery/file_downloader"),space);
	}
};

exports.functions={
	/*
		Function: functions.get_file_group_id

		Gets file_group_id from either one - code, name or id of file_group_id

		Parameters:
			options - the hash of:
			options.PREFIX_id - (optional)
			options.PREFIX_code - (optional)
			options.PREFIX_name - (optional)
			prefix - a prefix to use in search of options

		Returns:
			Numeric ID or undefined
	*/
	get_file_group_id:function(space,options,prefix) {
		if (!prefix) prefix="folder";
		if (options[prefix+"_id"]) return options[prefix+"_id"];
		if (options[prefix+"_code"]) return space.site.sql.execute_and_fetch_one("file_groups/get_by_code",{code: options[prefix+"_code"]}).id;
		if (options[prefix+"_name"]) return space.site.sql.execute_and_fetch_one("file_groups/get_by_name",{name: options[prefix+"_name"]}).id;
		return undefined;
	}
};
